﻿using UnityEngine;
using UnityEditor;

public class ExportAssetBundles
{
    //在Unity编辑器中添加菜单
    [MenuItem("Assets/Build AssetBundle From Selection")]
    static void ExportResourceRGB2()
    {
        // 打开保存面板，获得用户选择的路径
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");

        if (path.Length != 0)
        {
            // 选择的要保存的对象
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            //打包
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
        }
    }

    [MenuItem("Assets/Save Scene")]
    static void ExportScene()
    {
        // 打开保存面板，获得用户选择的路径
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");

        if (path.Length != 0)
        {
            // 选择的要保存的对象
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            string[] scenes = {"Assets/scene1.unity"};
            //打包
            BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);
        }
    }

    [MenuItem("Assets/BuildAssetBundle")]
    static void Execute()
    {
        string SavePath = "C:\\";

        BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
            | BuildAssetBundleOptions.DeterministicAssetBundle;

        BuildPipeline.PushAssetDependencies();
        // 共享资源red.mat
        Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Materials/red.mat");
        BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        // Prefab1，引用了red.mat
        BuildPipeline.PushAssetDependencies();
        Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Cube.prefab");
        BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
        BuildPipeline.PopAssetDependencies();

        // Prefab2，引用了red.mat
        BuildPipeline.PushAssetDependencies();
        Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Sphere.prefab");
        BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
        BuildPipeline.PopAssetDependencies();

        BuildPipeline.PopAssetDependencies();

        Object p3Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Plane.prefab");
        BuildPipeline.BuildAssetBundle(p3Asset, null, SavePath + p3Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        Object p4Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Tank.prefab");
        BuildPipeline.BuildAssetBundle(p4Asset, null, SavePath + p4Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        EditorUtility.DisplayDialog("", "Completed", "OK");
        AssetDatabase.Refresh();
    }

}